package com.cloudfun.campusshare.util;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * Created by Huqin on 2020/1/17
 */
public class EnumStringValidator implements ConstraintValidator<EnumString, String> {

    /**
     * 可选值
     */
    String[] values;

    @Override
    public void initialize(EnumString constraintAnnotation) {
        values = constraintAnnotation.values();
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (StringUtils.isNotBlank(value) && ArrayUtils.isNotEmpty(values)) {
            return ArrayUtils.contains(values, value);
        }
        return true;
    }
}
